<template>
  <el-dialog :title="'调整店铺、员工最大数量'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="150px">
      <h1>负数代表无限制</h1>
      <el-form-item label="最大店铺数量" prop="maxShopNum">
        <el-input v-model="dataForm.maxShopNum" placeholder="最大店铺数量，负数代表无限制" maxlength="5"></el-input>
      </el-form-item>
      <el-form-item label="店铺最大员工数量" prop="maxEmployeeNum">
        <el-input v-model="dataForm.maxEmployeeNum" placeholder="店铺最大员工数量" maxlength="5"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: null,
          maxShopNum: null,
          maxEmployeeNum: null
        },
        dataRule: {
          maxShopNum: [{
            required: true,
            message: '请输入最大店铺数量',
            trigger: 'blur'
          }],
          maxEmployeeNum: [{
            required: true,
            message: '请输入店铺最大员工数量',
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      init(company) {
        this.dataForm = {
          id: company.id,
          maxShopNum: company.maxShopNum,
          maxEmployeeNum: company.maxEmployeeNum
        }
        this.visible = true; 
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.disabled = true;
            this.$http({
              url: this.$http.adornUrl(`/merchant/company/update-num`),
              method: 'post',
              data: this.$http.adornData({
                id: this.dataForm.id || undefined,
                maxShopNum: this.dataForm.maxShopNum,
                maxEmployeeNum: this.dataForm.maxEmployeeNum
              })
            }).then(({
              data
            }) => {
              this.disabled = false;
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        });
      }
    }
  };
</script>
